package com.example.play.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.image.glide.GlideImgLoader
import com.example.play.databinding.ItemVideoPlayLayoutBinding
import com.example.play.entity.MovieVideoEntity

class MovieVideoListAdapter constructor(private val list:List<MovieVideoEntity>): RecyclerView.Adapter<MovieVideoListAdapter.ItemVideoHolder>() {

    private var itemClickListener: ItemClickListener ?= null

    inner class ItemVideoHolder(val dataBinding: ItemVideoPlayLayoutBinding): RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVideoHolder {
        return ItemVideoHolder(ItemVideoPlayLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    interface ItemClickListener{
        fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity)
    }
    fun setOnItemClickListener(l: ItemClickListener){
        itemClickListener = l
    }


    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: ItemVideoHolder, position: Int) {
        val entity=list[position]
        GlideImgLoader.loadCornerImage(holder.dataBinding.root.context,entity.videomainimag,holder.dataBinding.ivMovieImg,15)
        holder.dataBinding.apply {
            tvMovieDesc.text=entity.description
            tvPlayCount.text="${entity.userid}次播放"
        }
        holder.dataBinding.root.setOnClickListener{
            itemClickListener?.onItemChildClick(it, position, entity)
        }
    }

}